package com.changgou.order.mq.listener;


import com.changgou.order.pojo.Order;
import com.changgou.order.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 延时监听，监听延时队列
 */
@Component
@RabbitListener(queues = "orderListenerQueue")
public class DelayMessageListener {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void getDelayMessage(String message){
        //这里拿到信息需要查order的状态，已经支付不用管，
        // 如果是未支付，则关闭订单回滚库存
        String orderId = message;
        Order order = orderService.findById(orderId);
        String payStatus = order.getPayStatus();
        //如果订单状态是 0未支付，  2 支付失败  需要删除订单回滚库存
        if (payStatus.equals("0") || payStatus.equals("2")){
            orderService.delete(orderId);
        }
        System.out.println("监听到的消息"+message);
    }
}
